<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
			</div>
			<div class="right-panel">
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" stripe>
				<el-table-column label="ID" prop="id" width="50"></el-table-column>
				<el-table-column label="内容" prop="msg" width="250">
					<template #default="scope">
						<el-image v-if="scope.row.type == 'image'" :preview-src-list="[IMG_URL+scope.row.msg]"
      :initial-index="0" style="max-height: 200px;" fit="contain" :src="IMG_URL+scope.row.msg" mode=""></el-image>
						<p style="cursor: pointer;" v-else-if="['audio', 'video', '*'].includes(scope.row.type)" @click="open(IMG_URL+scope.row.msg)">{{IMG_URL+scope.row.msg}}</p>
						<p v-else>{{scope.row.msg}}</p>
					</template>
				</el-table-column>
				<el-table-column label="类型" prop="type" width="150">
					<template #default="scope">
						<p >{{types[scope.row.type]}}</p>
					</template>
				</el-table-column>
				<el-table-column label="计划发送时间" prop="send_time" width="150">
					<template #default="scope">
						<p >{{$TOOL.dateFormat(scope.row.send_time*1000)}}</p>
					</template>
				</el-table-column>
				<el-table-column label="第三方任务ID" prop="task_id" width="180"></el-table-column>
				<el-table-column label="发送状态" prop="state" width="150">
					<template #default="scope">
						<p >{{states[scope.row.state]}}</p>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="100">
					<template #default="scope">
						<el-button type="primary" plain size="small" @click="table_edit(scope.row)">编辑</el-button>
						<!-- <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button text type="primary" size="small">删除</el-button>
							</template>
						</el-popconfirm> -->
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>


</template>

<script>
	import saveDialog from './components/msgtskSave.vue'
import configIndex from "@/config"
	export default {
		name: 'configureList',
		components: {
			saveDialog,
		},
		data() {
			return {
				dialog:{
					save: false,
				},
				search: {
					type: '',
					// receive_type: '',
					// use_type: '',
					// status: '',
				},
				list: {
					apiObj: this.$API.marketing.msgtsk.list
				},
				types: {'text': '文本', 'image': '图片', 'audio': '音频', 'video': '视频', '*': '其他'},
				IMG_URL: configIndex.IMG_URL,
				states: {'0': '未发送', '1': '已发送', '2': '任务加入队列', '3': '发送失败'}
			}
		},
		mounted() {

		},
		methods: {
			//删除
			async table_del(row){
				var reqData = {id: row.id}
				var res = await this.$API.marketing.msgtsk.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			},
			//搜索
			upsearch() {
				// console.log(112, this.search)
				let search = JSON.parse(JSON.stringify(this.search))
				this.$refs.table.upData({
					...search
				})
			},
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//窗口编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},

			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			},
			open(url) {
				window.open(url, '_blank')
			}
		}
	}
</script>

<style>
</style>
